<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Page extends Model
{
    use SoftDeletes;
    protected $table = 'blog_page';

    const SECRET_ALL  = 1;
    const SECRET_SINGLE = 0;

    const STATE_DRAFT = 0;
    const STATE_CHECK = 1;
    const STATE_PUBLISH = 2;

    const TYPE_PAGE = 1;
    const TYPE_POST = 2;

    const ROOT_PAGE = 1;

    public function getStateName($state = null)
    {
        if(empty($state)){
            $state = $this->state;
        }
        switch ($state){
            case self::STATE_DRAFT:
                return '草稿';
            case self::STATE_CHECK:
                return '待审核';
            case self::STATE_PUBLISH:
                return '已发布';
            default:
                return '未知';
        }
    }

    public function getTypeName($type = null){
        if(empty($type)){
            $type = $this->type;
        }
        switch ($type){
            case self::TYPE_PAGE:
                return '页面';
            case self::TYPE_POST:
                return '文章';
            default:
                return '未知';
        }
    }

    public function meta()
    {
        return $this->hasOne('App\Model\Meta','page_id','id');
    }

    public function getChilds()
    {
        return Page::where('id',$this->parent)->get();
    }

    public function getMeta()
    {
        return $this->hasOne('App\Model\Meta','page_id','id')->first();
    }

//    public function getUpdated_atAttribute($value){
//        return date('m',strtotime($value));
//    }
}
